/**
 * OSAmI: Open Source Ambient Intelligence
 * Copyright (C) 2010 Bull S.A.S.
 * Contact: osami-general@osami-commons.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 *
 * --------------------------------------------------------------------------
 * $Id: ChangePropertyIdentityOperation.java 380 2011-04-20 14:23:04Z osami.fr $
 * --------------------------------------------------------------------------
 */


package org.osami.gateway.display.internal.identity;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.Requires;
import org.osami.gateway.display.internal.ServletPaths;
import org.osami.gateway.identity.Identity;
import org.osami.gateway.identity.IdentityAlreadyBoundException;
import org.osami.gateway.identity.IdentityManager;

/**
 * A {@code ListIdentitiesOperation} is ...
 *
 * @author Guillaume Sauthier
 */
@Component
@Provides
@Instantiate
public class ChangePropertyIdentityOperation implements WebOperation {

    public static final String NEW_VALUE = "new-value";
    public static final String PATTERN = "/change";

    @Requires
    private IdentityManager manager;

    public String getRecognizedPattern() {
        return "/{id}" + PATTERN + "/{name}";
    }

    public void execute(final HttpServletRequest request,
                        final HttpServletResponse response,
                        final Map<String, String> urlParameters)
            throws ServletException, IOException {

        PrintWriter writer = response.getWriter();

        String id = urlParameters.get("id");
        Identity identity = this.manager.getIdentity(id);

        if (identity == null) {
            throw new ServletException("Unknown Identity : " + id);
        }

        String name = urlParameters.get("name");

        String value = request.getParameter(NEW_VALUE);

        if (value == null) {
            throw new ServletException("missing parameter : " + NEW_VALUE);
        }


        if ("bound".equals(name)) {
            // Beurk ...
            try {
                this.manager.bind(value, identity);
            } catch (IdentityAlreadyBoundException e) {
                throw new ServletException("Cannot bind identity to identifier", e);
            }
        } else {
            identity.changeProperty(name, value);
        }


        writer.println("<html lang='en'>");

        // HTML Head
        // ---------------------------------------------------
        writer.println("<head>");
        writer.println("<title>Change Property of Identity</title>");
        writer.println("<link href=\"/resource/css/designSensor.css\" rel=\"stylesheet\" type=\"text/css\" />");
        writer.println("</head>");

        // HTML Body
        // ---------------------------------------------------
        writer.println("<body>");

        printPageTitle(writer, identity);

        writer.println("<hr/>\n");
        printBackButton(writer, identity);
        writer.println("<a href=\"" + IdentitiesServlet.IDENTITY + ListIdentitiesOperation.PATTERN + "\">Go back to identities list page</a><br />\n");
        writer.println("<a href=\"" + IdentitiesServlet.IDENTITY + "\">Go back to main identity page</a><br />");
        writer.println("<a href=\"" + ServletPaths.INDEX + "\">Go back to main page</a>\n");

        writer.println("</body>");

        writer.println("</html>");
    }

    private void printBackButton(PrintWriter writer, Identity identity) {
        String action = IdentitiesServlet.IDENTITY + "/" + identity.getIdentityID() + IdentityDetailsOperation.PATTERN;
        writer.println("<form method='GET' action='" + action + "'>");
        writer.println("  <input type='submit' value='Back' />");
        writer.println("</form>");
    }

    private void printPageTitle(PrintWriter writer, Identity identity) {
        writer.println("<h1>Modified Identity : " + identity.getIdentityID() + "</h1>");
    }

}
